package net.tv90.appserveradmin.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import net.tv90.appserveradmin.model.Hospital;
import net.tv90.appserveradmin.service.HospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/hospitals")
public class HospitalController {

    @Autowired
    private HospitalService hospitalService;

    @SaCheckRole("admin")
    @GetMapping
    public String listHospitals(Model model) {
        List<Hospital> hospitalList = hospitalService.list();
        model.addAttribute("hospitals", hospitalList);
        return "hospitals/list";
    }

    @SaCheckRole("admin")
    @GetMapping("/add")
    public String addHospitalForm(Model model) {
        model.addAttribute("hospital", new Hospital());
        return "hospitals/add";
    }

    @SaCheckRole("admin")
    @PostMapping("/add")
    public String addHospital(@ModelAttribute Hospital hospital) {
        hospitalService.save(hospital);
        return "redirect:/hospitals";
    }

    @SaCheckRole("admin")
    @GetMapping("/edit/{id}")
    public String editHospitalForm(@PathVariable Integer id, Model model) {
        Hospital hospital = hospitalService.getById(id);
        model.addAttribute("hospital", hospital);
        return "hospitals/edit";
    }

    @SaCheckRole("admin")
    @PostMapping("/edit")
    public String editHospital(@ModelAttribute Hospital hospital) {
        hospitalService.updateById(hospital);
        return "redirect:/hospitals";
    }

    @SaCheckRole("admin")
    @GetMapping("/delete/{id}")
    public String deleteHospital(@PathVariable Integer id) {
        hospitalService.removeById(id);
        return "redirect:/hospitals";
    }

    @SaCheckRole("admin")
    @GetMapping("/view/{id}")
    public String viewHospital(@PathVariable Integer id, Model model) {
        Hospital hospital = hospitalService.getById(id);
        model.addAttribute("hospital", hospital);
        return "hospitals/view";
    }
}
